﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace ForumsBrowser.BusinessObjects
{
    public class Thread
    {
        public Thread()
        {
            Messages = new List<Message>();
            IsNew = true;
        }

        public Guid Id { get; set; }
        public string Title { get; set; }
        public User Author { get; set; }
        public string Url { get; set; }
        public int Replies { get; set; }
        public int Views { get; set; }
        public DateTime CreatedOn { get; set; }
        public string Type { get; set; }
        public List<Message> Messages { get; private set; }
        public User LastReplier { get; set; }
        public DateTime? LastRepliedOn { get; set; }
        public string ForumName { get; set; }

        public string LastMessageText
        {
            get
            {
                var msg = Messages.LastOrDefault();
                if (msg != null)
                    return msg.CleanedBody;
                return string.Empty;
            }
        }

        public User LastMessageBy
        {
            get
            {
                return LastReplier ?? Author;
            }
        }

        public bool HasReplies { get { return Replies > 0; } }

        public DateTime LastUpdate
        {
            get
            {
                return LastRepliedOn ?? CreatedOn;
            }
        }

        public bool IsComplete
        {
            get
            {
                return Messages.Count - 1 == Replies;
            }
        }

        public bool IsNew
        {
            get;
            set; 
        }

        public override bool Equals(object obj)
        {
            return obj != null && (obj is Thread) && (obj as Thread).Id == this.Id;
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }

        public bool Update(Thread other)
        {
            if (other != null && this.Id == other.Id)
            {
                this.Id = other.Id;
                this.Title = other.Title;
                this.Author = other.Author;
                this.Url = other.Url;
                if (this.Replies != other.Replies)
                    this.IsNew = true;
                this.Replies = other.Replies;
                this.Views = other.Views;
                this.CreatedOn = other.CreatedOn;
                this.Type = other.Type;
                this.Messages = other.Messages;
                this.LastReplier = other.LastReplier;
                this.LastRepliedOn = other.LastRepliedOn;
                if (!string.IsNullOrEmpty(other.ForumName))
                    this.ForumName = other.ForumName;

                return true;
            }
            else
            {
                return false;
            }
        }

        public string XmlUrl
        {
            get
            {
                return Url + "?outputas=xml";
            }
        }

        public override string ToString()
        {
            return this.Title;
        }
    }
}
